#!/usr/bin/env python
"""
PyLink-SR (Python Linked Software Radio System)
(c) Bob Cowdery (G3UKB) 2008

File: jack_connect.py
Date: July 2008

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The author can be reached by email at

bob@g3ukb.co.uk

========================================================================
Module Description:

Manage the Pyro Services

"""

#-----------------------------------------------------------------------
# System imports
from time import sleep
from os import kill
import signal
import subprocess
import Pyro.core
import Pyro.naming

# Application imports
import config_service as config
import logger_service as logger

#---------------------------------------------------------------------------
class PyroServices:
    
    #-----------------------------------------------------------------------
    def __init__(self):
        # Pick the right host for local or remote operation
        if config.mode() == 'standalone':
            self.host = config.pyro_local_host()
        else:
            self.host = config.pyro_remote_host()
        
        self.daemon = None
        self.ns = None
        self.es = None
            
    #-----------------------------------------------------------------------
    def start_services(self):
        
        # Start the Pyro Name Service
        self.ns = subprocess.Popen(['pyro-ns', '-n ' + self.host])
        sleep(2)
        
        # Start the Pyro Event Service
        self.es = subprocess.Popen(['pyro-es', '-n ' + self.host])
        sleep(2)
 
#-----------------------------------------------------------------------
    def start_daemon(self):
    
        # Start the Pyro daemon
        Pyro.core.initServer()
        self.daemon=Pyro.core.Daemon(host=self.host)
        ns=Pyro.naming.NameServerLocator().getNS()
        self.daemon.useNameServer(ns)
        return self.daemon
    
    #-----------------------------------------------------------------------
    def stop_services(self):
        
         # Terminate the name service
        if self.ns <> None:
            pid = self.ns.pid
            kill(pid, signal.SIGTERM)
            # We must wait or it will get zombified
            self.ns.wait()
            
        # Terminate the event service
        if self.es <> None:
            pid = self.es.pid
            kill(pid, signal.SIGTERM)
            # We must wait or it will get zombified
            self.es.wait()

    #-----------------------------------------------------------------------
    def stop_daemon(self):
    
        # Stop the Pyro daemon
        if self.daemon <> None:
            self.daemon.shutdown(True)
        
    #-----------------------------------------------------------------------       
    def get_daemon(self):
        return self.daemon
    
    #-----------------------------------------------------------------------       
    def get_ns(self):
        return self.ns
    
    #-----------------------------------------------------------------------       
    def get_es(self):
        return self.es